package com.atguigu.eduservice.controller;

import com.alipay.api.domain.Article;
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.ArtSubject;
import com.atguigu.eduservice.entity.EduArticle;
import com.atguigu.eduservice.service.ArtSubjectService;
import com.atguigu.eduservice.service.ArticleService;
import com.atguigu.handler.GuiliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/eduservice/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private ArtSubjectService artSubjectService;


    //根据文章的分类查询

    @RequestMapping("getarticleByasubid/{asbid}")
    public R getarticleByasubid(@PathVariable String asbid) {
        List<EduArticle> articleList = articleService.list(new QueryWrapper<EduArticle>().eq("two_id", asbid));

        List list = new ArrayList();
        List list1 = new ArrayList();
        List ArtSubject = new ArrayList();
        HashMap<String, List> map = new HashMap<>();
        articleList.stream()
                .forEach(o -> {

                    String twoId = o.getTwoId();

                    ArtSubject artSubjectServiceById = artSubjectService.getById(twoId);
                    ArtSubject.add(artSubjectServiceById);

//                    String avatar = ucenterClient.getUserInfoOrder(o.getUserId()).getAvatar();

                    Optional<String> optional = Optional.ofNullable(ucenterClient.getUserInfoOrder(o.getUserId()).getAvatar());

                    if (optional.isPresent()) {
                        list.add(ucenterClient.getUserInfoOrder(o.getUserId()).getAvatar());

                    } else {
                        throw new GuiliException(20001, "远程调用失败");
                    }

                    String nickname = ucenterClient.getUserInfoOrder(o.getUserId()).getNickname();
                    list1.add(nickname);
                });


        map.put("avatars", list);
        map.put("nicknames", list1);
        map.put("ArtSubject", ArtSubject);


        System.err.println(articleList.size());

        return R.ok().data("total", articleList.size()).data("rows", articleList).data("map", map);
    }


    //查询所有的文章带分页
    @RequestMapping("getarticlelist/{page}/{limit}")
    public R getallarticle(@ApiParam(name = "page", value = "当前页码", required = true)
                           @PathVariable long page,
                           @ApiParam(name = "limit", value = "每页记录数", required = true)
                           @PathVariable long limit, HttpServletRequest request
    ) {

        Page<EduArticle> pageParam = new Page<>(page, limit);

        articleService.page(pageParam);
        List Avatars = Collections.synchronizedList(new ArrayList());
        List<String> nicknames = Collections.synchronizedList(new ArrayList());
        List<ArtSubject> ArtSubject = Collections.synchronizedList(new ArrayList());
        HashMap<String, List> map = new HashMap<>();
        List<EduArticle> records = pageParam.getRecords();

        records.stream().parallel().forEach(o -> {

            String twoId = o.getTwoId();

            ArtSubject artSubjectServiceById = artSubjectService.getById(twoId);
            ArtSubject.add(artSubjectServiceById);

//                    String avatar = ucenterClient.getUserInfoOrder(o.getUserId()).getAvatar();

            Optional<String> optional = Optional.ofNullable(ucenterClient.getUserInfoOrder(o.getUserId()).getAvatar());

            if (optional.isPresent()) {
                Avatars.add(ucenterClient.getUserInfoOrder(o.getUserId()).getAvatar());

            } else {
                throw new GuiliException(20001, "远程调用失败");
            }

            String nickname = ucenterClient.getUserInfoOrder(o.getUserId()).getNickname();
            nicknames.add(nickname);
        });
        map.put("avatars", Avatars);
        map.put("nicknames", nicknames);
        map.put("ArtSubject", ArtSubject);
        //总记录数
        long total = pageParam.getTotal();

        return R.ok().data("total", total).data("rows", records).data("map", map);

    }


    //添加文章
    @RequestMapping("addArticle")
    public R addarticle(@RequestBody EduArticle eduArticle, HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        eduArticle.setUserId(id);
        boolean save = articleService.save(eduArticle);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

//根据id查询文章

    @RequestMapping("getarticleByid/{aid}")
    public R getarticleByid(@PathVariable String aid) {
        EduArticle articleServiceById = articleService.getById(aid);
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(articleServiceById.getUserId());
        String nickname = userInfoOrder.getNickname();

        String avatar = userInfoOrder.getAvatar();
        HashMap<String, List> map = new HashMap<>();

        ArrayList<Object> list = new ArrayList<>();

        list.add(nickname);
        list.add(avatar);

        list.add(articleServiceById);

        map.put("item", list);

        return R.ok().data("item", map);
    }


    @GetMapping("getuserinfo")
    public R getuserinfo(HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(uid);
        return R.ok().data("userinfo", userInfoOrder);
    }
}
